//
//  LWZFirstOtherRowViewController.m
//  MusicFM
//
//  Created by 王景欣 on 16/1/19.
//  Copyright © 2016年 LWZ. All rights reserved.
//

#import "LWZFirstOtherRowViewController.h"

@interface LWZFirstOtherRowViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSString *stringUrl;

@property (nonatomic, strong) UIImageView *backImageView;

@end

@implementation LWZFirstOtherRowViewController

- (instancetype)initWithStringURL:(NSString *)stringURL
{
    self = [super init];
    if (self) {
        self.stringUrl = stringURL;

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_1494"]];
    self.backImageView.frame = self.view.bounds;
    [self.view addSubview:self.backImageView];
    
    self.view.backgroundColor = [UIColor clearColor];
    [self creatTableView];
    [self handleData];
}

#pragma mark - 创建tableView
- (void)creatTableView {
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49) style:UITableViewStyleGrouped];
    self.tableView.separatorColor = [UIColor colorWithRed:0.73 green:0.81 blue:0.96 alpha:1];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LWZRankTableViewCell" bundle:nil] forCellReuseIdentifier:@"LWZRankTableViewCellID"];
    
    [self.view addSubview:self.tableView];

}
#pragma mark - 处理数据
- (void)handleData {
    
    NSString *string = @"http://140.207.215.242/mobile/others/ca/album/track/%@/true/1/20?device=iPhone";
    self.stringUrl = [NSString stringWithFormat:string, self.stringUrl];
    
    [MHNetWorkTask getWithURL:self.stringUrl withParameter:nil withHttpHeader:nil withResponseType:ResponseTypeJSON withSuccess:^(id result) {
        
        NSArray *array = [NSArray arrayWithArray:[[result valueForKey:@"tracks"] valueForKey:@"list"]];
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in array) {
            LWZHostPlayModel *hostPlayModel = [[LWZHostPlayModel alloc] initWithDataSource:dic];
            [mutableArray addObject:hostPlayModel];
        }
        self.dataSource = [NSMutableArray arrayWithArray:mutableArray];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } withFail:^(NSError *error) {
        
    }];
}
#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;//
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LWZRankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZRankTableViewCellID"];
    cell.model = self.dataSource[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
    
    
}
#pragma mark - HEIGHT
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}
#pragma mark - TableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LWZFirstOtherRowModel *model = self.dataSource[indexPath.row];
    LWZFMPlayViewController *playVC = [LWZFMPlayViewController sharePlayerViewController];
    playVC.song_id = model.trackId;
    playVC.indexRow = indexPath.row;
    [self presentViewController:playVC animated:YES completion:nil];
}


#pragma mark --------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
